package com.rr.middle;

/**
 * @author zhuangcanrun
 * @Date 2021/12/02/0:45
 * @description
 **/
/*
 *https://leetcode-cn.com/problems/integer-to-roman/
 * 注：这算哪门子的中等难度
 * */
public class IntToRoman {
    public String intToRoman(int num) {
        int[] numArray = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
        String[] strArray = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
        StringBuilder sb = new StringBuilder();
        int index = 0;
        while(num>0){
            int tempNum = num - numArray[index];
            if(tempNum>0){
                sb.append(strArray[index]);
                num = tempNum;
            }
            if (tempNum == 0) {
                sb.append(strArray[index]);
                break;
            }
            if (tempNum < 0) {
                index++;
            }
        }
        return sb.toString();
    }
}
